package com.company.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.project.command.warehouse.WareHouseListCommand;
import com.company.project.command.warehouse.WarehouseCreateorUpdateCommand;
import com.company.project.common.utils.DataResult;
import com.company.project.entity.warehouse.Warehouse;
import com.company.project.service.HttpSessionService;
import com.company.project.service.warehouse.WareHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/warehouse")
public class WareHouseController {

    @Autowired
    private WareHouseService wareHouseService;

    @Autowired
    private HttpSessionService httpSessionService;

    /**
     * 查询
     * @param command
     * @return
     */
    @PostMapping("/list")
    public DataResult list(@RequestBody WareHouseListCommand command){
        IPage<Warehouse> wareResult = this.wareHouseService.selectAllTables(command);
        return DataResult.success(wareResult);
    }

    /**
     * 新增
     * @param command
     * @param bindingResult
     * @return
     */
    @PostMapping("/create")
    public DataResult create(@Validated @RequestBody WarehouseCreateorUpdateCommand command, BindingResult bindingResult) {
        command.validData(bindingResult);
        this.wareHouseService.create(command);
        return DataResult.success();
    }

    /**
     * 编辑页面展示
     * @param id
     * @return
     */
    @GetMapping("get/{id}")
    public DataResult update(@PathVariable("id") String id){
        return DataResult.success(this.wareHouseService.getById(id));
    }

    /**
     * 修改
     * @param id
     * @param command
     * @param bindingResult
     * @return
     */
    @PostMapping("/{id}/update")
    public DataResult update(@PathVariable("id") String id,
                             @Validated @RequestBody WarehouseCreateorUpdateCommand command,BindingResult bindingResult){
        command.validData(bindingResult);
        this.wareHouseService.update(id,command);
        return DataResult.success();
    }

    /**
     * 启用
     */
    @PostMapping("/{id}/enable")
    public DataResult enable(@PathVariable("id") String id){
        this.wareHouseService.enable(id,httpSessionService.getCurrentUsername());
        return DataResult.success();
    }


    /**
     * 禁用
     */
    @PostMapping("/{id}/disable")
    public DataResult disable(@PathVariable("id") String id){
        this.wareHouseService.disable(id,httpSessionService.getCurrentUsername());
        return DataResult.success();
    }


    /**
     * 删除
     */
    @PostMapping("/delete/{id}")
    public DataResult delete(@PathVariable("id") String id){
        this.wareHouseService.removeById(id);
        return DataResult.success();
    }
    /**
     * 获取下拉选
     */
    @PostMapping("/getOption")
    public DataResult getOption(){
        return DataResult.success(this.wareHouseService.getOption());
    }

}
